package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.designations.DesignationsTransfer;
import com.admin.education.dataTransferObject.icons.IconsTransfer;
import com.admin.education.dataTransferObject.icons.Mapper.IconsMapper;
import com.admin.education.dataTransferObject.icons.request.CreateIconsRequest;
import com.admin.education.dataTransferObject.icons.request.UpdateIconsRequest;
import com.admin.education.models.Designations;
import com.admin.education.models.Icons;
import com.admin.education.models.QIcons;
import com.admin.education.repositories.IconsRepository;
import com.admin.education.services.Interfaces.IIconsService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;

@Service
@Validated
@Transactional
public class IconsService implements IIconsService {
    private QIcons qIcons;
    @Autowired
    private IconsRepository iconsRepository;
    @Autowired
    private IconsMapper iconsMapper;
    public IconsService(){
        qIcons = QIcons.icons;
    }
    @Override
    public ServicesExcuteResult<List<IconsTransfer>> queryIcons() {
        ServicesExcuteResult<List<IconsTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<Icons> iconsJPAQuery = this.iconsRepository.selectFrom(qIcons);

        List<Icons> icons = iconsJPAQuery.fetch();

        List<IconsTransfer> iconsTransfers = iconsMapper.mapFrom(icons);

        servicesExcuteResult.setData(iconsTransfers);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<PageResponse<IconsTransfer>> queryIcons(@Valid PageRequest pageRequest) {
        ServicesExcuteResult<PageResponse<IconsTransfer>> servicesExcuteResult =new ServicesExcuteResult<>();

        JPAQuery<Icons> iconsJPAQuery = this.iconsRepository.selectFrom(qIcons);

        long totalCount = iconsJPAQuery.fetchCount();
        iconsJPAQuery = iconsJPAQuery.orderBy(qIcons.id.asc())
                .offset((pageRequest.getPageIndex()-1)*pageRequest.getPageSize()).limit(pageRequest.getPageSize());

        List<Icons> icons = iconsJPAQuery.fetch();

        List<IconsTransfer> iconsTransfers = this.iconsMapper.mapFrom(icons);
        PageResponse<IconsTransfer> iconsTransferPageResponse = new PageResponse<>(pageRequest.getPageSize(),pageRequest.getPageIndex(),(int)totalCount,iconsTransfers);

        servicesExcuteResult.setData(iconsTransferPageResponse);
        servicesExcuteResult.setSuccess(true);

        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult createIcons(@Valid CreateIconsRequest createIconsRequest) {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Icons icons =this.iconsMapper.mapFrom(createIconsRequest);

        this.iconsRepository.persist(icons);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateIcons(long iconsId, @Valid UpdateIconsRequest updateIconsRequest) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Icons> iconsJPAQuery = this.iconsRepository.selectFrom(qIcons).where(qIcons.id.eq(iconsId));
        if(0!=iconsJPAQuery.fetchCount()){
            Icons icons = iconsJPAQuery.fetchOne();

            icons =this.iconsMapper.mapFrom(updateIconsRequest,icons);

            this.iconsRepository.merge(icons);
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("首页图标不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<IconsTransfer> queryIconsById(long iconsId) throws ResourceNotFoundException {
        ServicesExcuteResult<IconsTransfer> servicesExcuteResult= new ServicesExcuteResult();

        JPAQuery<Icons> iconsJPAQuery = this.iconsRepository.selectFrom(qIcons).where(qIcons.id.eq(iconsId));
        if(0!=iconsJPAQuery.fetchCount()){
            Icons icons = iconsJPAQuery.fetchOne();

            servicesExcuteResult.setData(this.iconsMapper.mapFrom(icons));
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("首页图标不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult deleteIcons(long iconsId) throws ResourceNotFoundException {
        ServicesExcuteResult<IconsTransfer> servicesExcuteResult= new ServicesExcuteResult();

        JPAQuery<Icons> iconsJPAQuery = this.iconsRepository.selectFrom(qIcons).where(qIcons.id.eq(iconsId));
        if(0!=iconsJPAQuery.fetchCount()){
            Icons icons = iconsJPAQuery.fetchOne();

            this.iconsRepository.remove(icons);
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("首页图标不存在");
        }
        return servicesExcuteResult;
    }
}
